package com.ciko.code.http;

import com.ciko.code.utils.LogUtils;
import com.ciko.code.utils.ToastUtils;

import java.io.Serializable;

public class BaseResponse<T> implements Serializable{

    private static final int SUCCESS_CODE = 1000;
    private final String TAG = "错误处理"+this.getClass().getSimpleName();

    // 错误码
    protected int resultCode;
    // 响应数据
    protected T returnObject;

    //无用三人组
    protected String resultCid;
    protected String returnMsg;
    protected int remainderNum;

    public int getResultCode() {
        return resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public boolean isSuccess() {
        return resultCode == SUCCESS_CODE;
    }

    public void isError(Integer resultCode) {
        switch (resultCode) {

            case 9999:
                showErrorMsg("系统异常或故障");
                break;
            case 9998:
                showErrorMsg("时间戳过期（15分钟）");
                break;
            case 9997:
                showErrorMsg("账号被锁定");
                break;
            case 9996:
                showErrorMsg("找回密码时账号被锁定");
                break;
            case 9995:
                showErrorMsg("找回密码验证码错误");
                break;
            case 9994:
                showErrorMsg("找回密码验证码过期");
                break;
            case 9993:
                showErrorMsg("找回密码非法请求，未按流程请求接口");
                break;
            case 9992:
                showErrorMsg("账号未登录");
                break;

            case 1001:
                showErrorMsg("必填参数存在空值");
                break;
            case 1002:
                showErrorMsg("参数长度、格式不合法");
                break;
            case 1003:
                showErrorMsg("非法请求（cid校验不合法）");
                break;
            case 1004:
                showErrorMsg("根据用户账号获取企业信息失败");
                break;

            case 1006:
                showErrorMsg("非法的文件类型");
                break;
            case 1007:
                showErrorMsg("文件大小超过最大限定值");
                break;
            case 1008:
                showErrorMsg("短信递交失败");
                break;
            case 1009:
                showErrorMsg("短信请求过于频繁");
                break;

            case 1100:
                showErrorMsg("用户不存在");
                break;
            case 1101:
                showErrorMsg("用户状态不正确");
                break;
            case 1102:
                showErrorMsg("用户密码不正确");
                break;
            case 1103:
                showErrorMsg("用户修改信息失败");
                break;
            case 1104:
                showErrorMsg("用户头像上传读取客户端文件失败");
                break;

        }
    }

    private void showErrorMsg(String errMsg) {
        LogUtils.e(TAG, errMsg);
        ToastUtils.showShort(TAG + errMsg);
    }

    public T getReturnObject() {
        return (T)returnObject;
    }


    @Override
    public String toString() {
        return "BaseResponse{" +
                "TAG='" + TAG + '\'' +
                ", resultCode=" + resultCode +
                ", returnObject=" + returnObject +
                ", resultCid='" + resultCid + '\'' +
                ", returnMsg='" + returnMsg + '\'' +
                ", remainderNum=" + remainderNum +
                '}';
    }
}

